package com.ztmg.water.service.water.impl;

import com.ztmg.water.model.water.Water;
import com.ztmg.water.vo.water.WaterVO;
import com.ztmg.water.dto.water.WaterDTO;
import com.ztmg.water.dto.water.WaterPageDTO;
import com.ztmg.common.util.resultUtils.JsonResult;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ztmg.common.constant.ErrorConst;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import com.ztmg.water.service.water.WaterService;
import com.ztmg.water.mapper.water.WaterMapper;import com.ztmg.water.model.water.WaterExample;

@Service
public class WaterServiceImpl implements WaterService{

    @Resource
    private WaterMapper waterMapper;
    @Override
    public JsonResult create(WaterDTO dto) {
        if(null != dto){
            Water bean = new Water();
            BeanUtils.copyProperties(dto,bean);
            bean.setCreateTime(new Date());
            //后续完善
            bean.setCreater("");
            waterMapper.insert(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult update(WaterDTO dto) {
        if(null != dto && null != dto.getId()){
            Water bean = new Water();
            BeanUtils.copyProperties(dto,bean);
            bean.setModifyTime(new Date());
            //后续完善
            bean.setModifier("");
            waterMapper.updateByPrimaryKeySelective(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<WaterVO> selectById(Integer id) {
        if(null != id){
            Water bean = waterMapper.selectByPrimaryKey(id);
            WaterVO vo = new WaterVO();
                if(null != bean){
                BeanUtils.copyProperties(bean,vo);
            }else {
                return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
            }
            return JsonResult.success(vo);
        }else{
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<List<WaterVO>> select(WaterDTO dto) {
        WaterExample example = new WaterExample();
        WaterExample.Criteria criteria =  example.createCriteria();
        if(dto.getWaterBrandId()!=null)
        {
            criteria.andWaterBrandIdEqualTo(dto.getWaterBrandId());
        }
        if(dto.getSupplierId() !=null)
        {
            criteria.andSupplierIdEqualTo(String.valueOf(dto.getSupplierId()));
        }
        /***
        * 根据具体的业务，查询条件赋值
         */
        List<Water> sysUserList = waterMapper.selectByExample(example);
        List<WaterVO> respDTOS = sysUserList.parallelStream().map((bean) -> {
            WaterVO vo = new WaterVO();
            BeanUtils.copyProperties(bean, vo);
            return vo;
        }).collect(Collectors.toList());
        return JsonResult.success(respDTOS);
    }

    @Override
    public JsonResult<List<WaterVO>> listAll(WaterDTO dto) {
        WaterExample example = new WaterExample();
        WaterExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(1);
        List<Water> beanList = waterMapper.selectByExample(example);
        List<WaterVO> respDTOS = new ArrayList<>();
        if(!beanList.isEmpty()){
             respDTOS = beanList.parallelStream().map((bean) -> {
                WaterVO vo = new WaterVO();
                BeanUtils.copyProperties(bean, vo);
                return vo;
            }).collect(Collectors.toList());
        }
        return JsonResult.success(respDTOS);
    }

    @Override
    public JsonResult<PageInfo<WaterVO>> selectPage(WaterPageDTO dto) {
        WaterExample example = new WaterExample();
        WaterExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<Water> beanList = waterMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo<>(beanList);
        List<WaterVO> respDTOS = beanList.parallelStream().map((bean) -> {
            WaterVO vo = new WaterVO();
            BeanUtils.copyProperties(bean, vo);
            return vo;
        }).collect(Collectors.toList());
        pageInfo.setList(respDTOS);
        return JsonResult.success(pageInfo);
    }

    @Override
    public JsonResult deleteById(Integer id) {
        return JsonResult.success( waterMapper.deleteByPrimaryKey(id));
    }

    @Override
    public JsonResult delete(WaterDTO dto) {
        WaterExample example = new WaterExample();
        WaterExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        return JsonResult.success(waterMapper.deleteByExample(example));
    }
}